-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add isolated and simplified path to add components #1204
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prevents the next commits ODR violation
inline script component ComponentType
Entity comes first always
Allows for much easier adding of components and is error proof by not allowing the user to add more than 1 of a specific component type to an Entity.
Move all to the new variadic templates AddComponent function to reduce clutter and ways the component map is modified. The new function makes no assumptions. Component is assumed to not exist and is checked for with operator[]. This will construct a null component which will then be newed if the component didnt exist, or it will just get the current component if it does already exist. No new component will be allocated or constructed if the component already exists and the already existing pointer is returned instead.
For the case where the component may already exist, use a placement new to construct the component again, it would be constructed again, but would not need to go through the allocator.
aronwk-aaron
previously approved these changes
Sep 30, 2023
Jettford
previously approved these changes
Oct 9, 2023
aronwk-aaron
previously approved these changes
Oct 9, 2023
Need to add manual destructor call to placement newd component. |
aronwk-aaron
previously approved these changes
Oct 12, 2023
Update Entity.cpp
EmosewaMC
force-pushed
the
somethingElse
branch
from
October 21, 2023 20:30
504b9ef
to
e22d342
Compare
aronwk-aaron
approved these changes
Oct 21, 2023
Jettford
approved these changes
Oct 23, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You scare me.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Follows a structure similar to std::thread in that you say what component you want to add in the Template types, and what you want to pass to the constructor in the rest of the variadic template values. This is a catch all solution and due to forwarding, is basically a free function call idenitcal to the old ones, but now all the newing and inserting is done in one place! Even replacing the component with a new one is all done at once.
before:
This had to be repeated for every component and is prone to error as well as weak to large changes (as you'll see in future changes)
after:
since the component always needs to know its parent Entity, we always default the first argument to being the Entity and dont make the user specify this. Then the other 3 arguments have their template types deduced and the component is also added to the map for you, all in 1 step. If you try to add a component again, instead of deleting the previous one, we simply use a placement new to basically reset the current pointer to its constructed state. This allows us to skip a delete and free and keeps the head code path at minimal branches (1 in our code and the normal amount in the map insert anyways).
A small test for the placement new is attached as well.
Tested that I could load into Venture Explorer, Forbidden Valley Race, Cannon Cove Shooting Gallery and Crux Prime and that the tests pass.